package develop.iforce.jsend;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class C2dmSend{

	private String SEND_URL = "https://android.apis.google.com/c2dm/send";
	private String AUTHORZATION = "GoogleLogin auth=DQAAALwAAACfhiccPOrFxk3__D7ccd-H-gUVwRZ9R_uXN979x_6iaMCCWdSQkPtVXTVnFKPbppeyIquoLcUstBSwU5xjOIuDFyLmGwF8sR1AtFQaGMfi-MVFv3Tjs3G4jlMgn__fV2ajKqFPZXsXMgEImz9GH6C0hnD_twUg87ZIVUeoANx4oljvlx_ztF0nsJ7-gC2-w57M5rJAF8_pyZrMFU9sqO-28z9cLNZJai7T-pZDMmgA9xJhEmD2_K_z1SWBVQVPtos";

	//for IS03(テスト機)
	//private String REGISTRATION_ID = "APA91bFyW0NTWgHe5FKFUQa0I4fDYDSZXBDXGbgpL8dmeu5ssZQ6yB6WYk5Bk0O80w-CFEvzDHs9pOEKAGTXcFAw0QG5rCLdqcEonz3NlopGVB4A0mmE_U_A1bvhY0y9FyPX9OvJyiTqPwD6bildnwC8PIVf6XeFeg";

	//for IS04(テスト機)
	private String REGISTRATION_ID = "APA91bEVXK2DB2x1tBSv78ZeCT4YOt_OsnUe5Qjody2R4r98G-ZMI2BUrHZQGcOyFGZNTUTvJAvrN8Ub3rU0EiJeol-2BxSkYFMPE1-Plo19JXaid2bfoZRkIkPVO0nDJTjwO_Q_H9tFztJ4HYW2BQjejeZyBRioZg";

	//for SO-01C(熊川)
	//public String REGISTRATION_ID  = "APA91bFtYJUw3VS6gWC6sCIDJRTdgRfy6cebUn3Yfzqz7yWAxra-yO_xV0dh7irocLxWT79xU_FIZJNQ99J45KzQwXSW1pwOMZNXGRMUBfq80mg7BVCCGegfuI-LMuD_juZO-zI2ujQlnc42NPyqlAlRzQ1_QzwqNQ";

	//for L-07C(白井さん)
	//public String REGISTRATION_ID  = "APA91bGPZYlVb_3WE684ARV4UASEthuKzCV87oravYhao4mRi70i5ePu-sKisVtLrCQxkmdYCvt6AIANX4IeaHhuhkeUduEJZRledP1kr1nI2ua2r38UuZ8Bc5yAGUxb_9QnPYSbCwLcPQEM0ETI0pIZMGCkQmnJ6g";

	//for SO-02C(相馬さん)
	//public String REGISTRATION_ID  = "APA91bHu6xah-3FUpReDi0LzY7eCqdg9ordMhLANAl3gKtnKhHV2GrNFWsujpZfssHkI2mzaptTqqVt754hf8vdU-pPnbplp8PPcM2mINIDqFIAlSl_6mo-4JttbSU36uvCIut65RuvxBshP5BFooBvYSjXuN_azhQ";

	public String message;
	public String Tsunami;
	public String Level;
	public String Sec;

	public String SendMessage(String type){

    	String resStr = "";

    	ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
    	params.add(new BasicNameValuePair("registration_id", REGISTRATION_ID));
    	params.add(new BasicNameValuePair("collapse_key", "1"));
    	params.add(new BasicNameValuePair("data.type", type));
    	if(type.equals("message")){
        	params.add(new BasicNameValuePair("data.message", message));
    	}else if(type.equals("earthquake")){
    		params.add(new BasicNameValuePair("data.level", Level));
    		params.add(new BasicNameValuePair("data.sec", Sec));
    		params.add(new BasicNameValuePair("data.tsunami",Tsunami));
        }


    	HttpPost httpPost = new HttpPost(SEND_URL);

    	try {
			httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

			httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");// ヘッダを設定
			httpPost.setHeader("Authorization ", AUTHORZATION);// ヘッダを設定

			DefaultHttpClient client = new DefaultHttpClient();
	    	HttpResponse httpResponse = client.execute(httpPost);

	    	int statusCode = httpResponse.getStatusLine().getStatusCode();

	    	HttpEntity entity = httpResponse.getEntity();
	    	String response = EntityUtils.toString(entity);

	    	entity.consumeContent();

	    	client.getConnectionManager().shutdown();

	    	resStr = statusCode +":"+response;

		} catch (UnsupportedEncodingException e1) {
			// TODO 自動生成された catch ブロック
			e1.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO 自動生成された catch ブロック
			e.printStackTrace();
		} catch (IOException e) {
			// TODO 自動生成された catch ブロック
			e.printStackTrace();
		}

		return resStr;

	}
}